平常我們在安裝第三方庫時,有些會提供兩種安裝方式讓使用者選擇,一種是從原始碼編譯後再安裝至系統,另一種是使用 ubuntu 的包管理器 dpkg ,這兩種方式各有各的優缺點。
使用CPack其實很簡單,核心只有兩個步驟就可以將庫或可執行程式打包成 deb 安裝檔:
設定軟體包的屬性(例如:套件名稱,套件描述,作者等資訊)
${CPACK_PACKAGE_NAME} : 包的名稱
${CPACK_GENERATOR} : 指定生成的格式 deb, 7z, zip ...
${CPACK_PACKAGE_VERSION} : 設定包的版本
${CPACK_PACKAGE_CONTACT} : 作者的聯繫方式
${CPACK_PACKAGE_DESCRIPTION} : 描述這個包的文字訊息
還有其他屬性可以設定,目前只介紹最常用的。
指定需要打包的文件(例如:可執行文件,靜態庫,動態庫)
include(InstallRequiredSystemLibraries)
# 將CPACK包含進項目中,CPake的開頭
...CPack的屬性
include(CPack)
# CPack的結尾
$ dpkg [-i | --install] [package-name]
安裝名為package-name的安裝檔
$ dpkg [-r | --remove] [package-name]
解除安裝名為package-name的安裝檔
$ dpkg [-l | --list]
列出安裝過的包
$ dpkg [-I | --info] [package-name]
印出安裝資訊
$ git clone https://github.com/m11112089/2023_iT_CMake.git
$ cd ~/2023_iT_CMake/Day16
編寫CMakeListx.txt
include(InstallRequiredSystemLibraries)
# 將CPACK包含進項目中,CPake的開頭
set(CPACK_PACKAGE_NAME ${CMAKE_PROJECT_NAME})
# 設定 deb 包名稱和項目相同
set(CPACK_GENERATOR "DEB")
# 設定CPACK_GENERATOR為DEB,即生成deb安裝包
set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
# 設定版本號和本項目相同
set(CPACK_PACKAGE_CONTACT "your_email.gmail.com")
# 作者的聯繫方式
set(CPACK_PACKAGE_DESCRIPTION "A C++ library for precise square root calculations using Newton's method.")
include(CPack)
# CPack的結尾
2. 編譯
$ cd build
$ cmake ..
$ make
3. 打包安裝檔
$ make package
kai@esoc:~/2023_iT_CMake/Day16/MathFunctions/build$ make package
Consolidate compiler generated dependencies of target mysqrt
[100%] Built target mysqrt
Run CPack packaging tool...
CPack: Create package using DEB
CPack: Install projects
CPack: - Run preinstall target for: MathFunctions
CPack: - Install project: MathFunctions []
CPack: Create package
-- CPACK_DEBIAN_PACKAGE_DEPENDS not set, the package will have no dependencies.
CPack: - package: /home/kai/2023_iT_CMake/Day16/MathFunctions/build/MathFunctions-1.0.0-Linux.deb generated.
4. 印出剛剛安裝的包資訊,驗證在CMakeLists.txt設定過的資訊
$ dpkg --info MathFunctions-1.0.0-Linux.deb
kai@esoc:~/2023_iT_CMake/Day16/MathFunctions/build$ dpkg --info MathFunctions-1.0.0-Linux.deb
new Debian package, version 2.0.
size 3598 bytes: control archive=387 bytes.
232 bytes, 9 lines control
128 bytes, 2 lines md5sums
Architecture: amd64
Description: A C++ library for precise square root calculations using Newton's method.
Maintainer: your_email.gmail.com
Package: mathfunctions
Priority: optional
Section: devel
Version: 1.0.0
Installed-Size: 64
5. 使用包管理器安裝
$ sudo dpkg --install MathFunctions-1.0.0-Linux.deb
kai@esoc:~/2023_iT_CMake/Day16/MathFunctions/build$ sudo dpkg -i MathFunctions-1.0.0-Linux.deb
[sudo] password for kai:
Selecting previously unselected package mathfunctions.
(Reading database ... 517153 files and directories currently installed.)
Preparing to unpack MathFunctions-1.0.0-Linux.deb ...
Unpacking mathfunctions (1.0.0) ...
Setting up mathfunctions (1.0.0) ...
6. 查看剛剛安裝的包
$ dpkg --list
ii man-db 2.10.2-1
ii manpages 5.10-1ubuntu1
ii manpages-dev 5.10-1ubuntu1
ii mathfunctions <------在這裡 ⭐ 1.0.0
ii mawk 1.3
ii media-player-info 24-2
7. 查看安裝的檔案
kai@esoc:/usr/local/include$ tree -L 1
.
├── librealsense2
├── librealsense2-gl
└── mysqrt.h <-----在這裡⭐
kai@esoc:/usr/local/bin$ tree -L 1
.
├── bochs
├── bximage
├── docker-compose -> /usr/lib/docker/cli-plugins/docker-compose
├── libmysqrt.so -> libmysqrt.so.1 <-----在這裡⭐
├── libmysqrt.so.1 -> libmysqrt.so.1.0.0 <-----在這裡⭐
├── libmysqrt.so.1.0.0 <-----在這裡⭐
8. 解除安裝
$ sudo dpkg --remove mathfunctions
kai@esoc:~$ sudo dpkg --remove mathfunctions
(Reading database ... 517158 files and directories currently installed.)
Removing mathfunctions (1.0.0) ...
dpkg: warning: while removing mathfunctions, directory '/usr/local/include' not empty so not removed
第二十一章、軟體安裝:原始碼與 Tarball
Why package installed by compiling its source code does not appear in dpkg -l output?